﻿Imports System.Data.SqlServerCe

Public Class SharedItems
    Shared Sub New()
        Dim aSelectColumns As String() = {"Id", "Name", "UniqueId", "Added", "SourceWatchFolderId" _
                                        , "LastChanged", "Directory", "TotalFiles", "TotalSize" _
                                        , "GroupMode", "ItemType", "HasMediaInfo"}
        ObjectPersister.DefineObject(GetType(Entities.SharedItem), "SharedItems", aSelectColumns)

    End Sub
    Private Sub New()
    End Sub

    Public Shared Function GetAllBySourceWatchFolderId(ByVal sourceWatchFolderId As Integer) As Generic.List(Of Entities.SharedItem)
        Return ObjectPersister.GetAll(Of Entities.SharedItem)(String.Format("SourceWatchFolderId={0}", sourceWatchFolderId))
    End Function
    Public Shared Function GetAllByDirectory(ByVal directory As String) As Generic.List(Of Entities.SharedItem)
        Return ObjectPersister.GetAll(Of Entities.SharedItem)(String.Format("Directory='{0}'", directory))
    End Function
    Public Shared Function GetById(ByVal id As Integer) As Entities.SharedItem
        Return ObjectPersister.GetOne(Of Entities.SharedItem)(String.Format("Id={0}", id))
    End Function

    Public Shared Function GetAllMoviesWithMissingOrOutDatedMediaInfo() As Generic.List(Of Entities.SharedItem)
        '        Dim strClause As String = "ItemType=0 AND (HasMediaInfo=0 OR HasMediaInfo IS NULL)"
        Dim strClause As String = "ItemType=0 AND Id IN (SELECT SharedItemId FROM MediaInfo WHERE Version<2) OR Id NOT IN (SELECT SharedItemId FROM MediaInfo)"
        Return ObjectPersister.GetAll(Of Entities.SharedItem)(strClause)
    End Function
    Public Shared Function GetAllMoviesWithoutGenres() As Generic.List(Of Entities.SharedItem)
        'Dim strClause As String = "ItemType=0 AND (HasMediaInfo=0 OR HasMediaInfo IS NULL)"
        Dim strClause As String = "ItemType=0 AND (HasMediaInfo=0 OR HasMediaInfo IS NULL OR Id IN (SELECT SharedItemId FROM MediaInfo WHERE Genres IS NULL))"
        Return ObjectPersister.GetAll(Of Entities.SharedItem)(strClause)
    End Function

    Public Shared Sub Delete(ByVal item As Entities.SharedItem, ByVal transactionScope As TransactionScope)
        Dim strSql As String = String.Format("DELETE FROM SharedItems WHERE Id={0}", item.Id)
        Environment.ExecuteNonQuery(strSql, transactionScope)

        'now, delete all shared files that correspond to this shared item
        SharedItemsFiles.DeleteBySharedItemId(item.Id, transactionScope)

        'also delete movieinfo, if this was a movie
        If item.HasMediaInfo Then ' item.ItemType = Entities.ItemType.Movies Then
            MediaInfo.Delete(item.Id, transactionScope)
        End If
    End Sub

    Public Shared Sub Save(ByVal item As Entities.SharedItem)
        Using t As New TransactionScope
            Save(item, t)
            t.Complete()
        End Using
    End Sub

    Public Shared Sub Save(ByVal item As Entities.SharedItem, ByVal transactionScope As DataStore.TransactionScope)
        Dim clsCmd As New SaveCommand(Of Integer)("SharedItems", "Id", item.Id, 0)
        clsCmd.AddParam("Added", item.Added, False)
        clsCmd.AddParam("Directory", item.Directory, False)
        clsCmd.AddParam("LastChanged", item.LastChanged, False)
        clsCmd.AddParam("Name", item.Name, False)
        clsCmd.AddParam("SourceWatchFolderId", item.SourceWatchFolderId, False)
        clsCmd.AddParam("UniqueId", item.UniqueId, False)

        clsCmd.AddParam("TotalFiles", item.TotalFiles, False)
        clsCmd.AddParam("TotalSize", item.TotalSize, False)
        clsCmd.AddParam("GroupMode", item.GroupMode, False)
        clsCmd.AddParam("ItemType", item.ItemType, False)
        clsCmd.AddParam("HasMediaInfo", item.HasMediaInfo, False)

        item.Id = clsCmd.Save(transactionScope)
    End Sub
End Class
